const express = require('express');
const router = express.Router();
const db = require('../db');

// 获取所有traffic_event数据
router.get('/', async (req, res) => {
	try {
		const { page, size, keyword, status } = req.query;
		const offset = (page - 1) * size;
		if (page && size && keyword) {
			let sql = `SELECT * FROM traffic_event where user_id = ${keyword} limit ${offset},${size}`;
			const data = await db.getAll(sql);
			if (data.length) {
				res.send({
					code: 0,
					message: '获取搜索用户成功',
					data,
				});
			} else {
				res.send({
					code: 2,
					message: '搜索用户不存在',
				});
			}
		} else if (status) {
			let sql = `SELECT * FROM traffic_event where event_handle_status like '%${status}%' limit ${offset},${size}`;
			const data = await db.getAll(sql);
			sql = `SELECT count(*) as total FROM traffic_event where event_handle_status like '%${status}%'`;
			const { total } = await db.getOne(sql);
			res.send({
				code: 0,
				message: '获取用户成功',
				total,
				data,
			});
		} else if (!page && !size && keyword) {
			let sql = `SELECT * FROM traffic_event where user_id = ${keyword}`;
			const data = await db.getAll(sql);
			res.send({
				code: 0,
				message: '获取用户事件成功',
				data,
			});
		} else {
			let sql = `SELECT * FROM traffic_event limit ${offset},${size}`;
			const data = await db.getAll(sql);
			sql = `SELECT count(*) as total FROM traffic_event`;
			const { total } = await db.getOne(sql);
			res.send({
				code: 0,
				total,
				data,
			});
		}
	} catch (error) {
		res.send({
			code: 500,
			message: '服务器错误',
		});
	}
});

// 获取单个traffic_event数据
router.get('/:id', async (req, res) => {
	try {
		const { id } = req.params;
		const sql = `SELECT * FROM traffic_event WHERE id=${id}`;
		const data = await db.getOne(sql);
		res.send({
			code: 0,
			data,
		});
	} catch (error) {
		res.send({
			code: 500,
			message: '服务器错误',
		});
	}
});

// 新增traffic_event数据
router.post('/', async (req, res) => {
	try {
		const {
			user_id,
			event_type,
			event_describe,
			road_section_id,
			event_driver,
			event_car_number,
		} = req.body;
		const sql = `INSERT INTO traffic_event (user_id, event_type, event_describe, road_section_id, event_driver, event_car_number) VALUES (${user_id}, '${event_type}', '${event_describe}', '${road_section_id}', '${event_driver}', '${event_car_number}')`;
		const data = await db.exec(sql);
		res.send({
			code: 0,
			message: '新增成功',
		});
	} catch (error) {
		res.send({
			code: 500,
			message: '服务器错误',
		});
	}
});

// 修改traffic_event数据
router.put('/:id', async (req, res) => {
	try {
		const { id } = req.params;
		const { event_handle_status } = req.body;
		const sql = `UPDATE traffic_event SET event_handle_status='${event_handle_status}' WHERE id=${id}`;
		const data = await db.exec(sql);
		res.send({
			code: 0,
			message: '修改成功',
		});
	} catch (error) {
		res.send({
			code: 500,
			message: '服务器错误',
		});
	}
});

// 删除traffic_event数据
router.delete('/:id', async (req, res) => {
	try {
		const { id } = req.params;
		const sql = `DELETE FROM traffic_event WHERE id=${id}`;
		const data = await db.exec(sql);
		res.send({
			code: 0,
			message: '删除成功',
		});
	} catch (error) {
		res.send({
			code: 500,
			message: '服务器错误',
		});
	}
});

module.exports = router;
